<?
if (preg_match("#" . basename(__FILE__) . "#", $_SERVER['PHP_SELF'])) {
	die('This page cannot be called directly.') ;
}

class ajax_comparehotel {

    function playme(){
        $code = Url::getParam('code');
        //yêu cầu đặt tên $code=tên của method trong class

        if (method_exists($this, $code)) {
            $this->$code();
        }
        else
        {
            $this->home();
        }
    }

    function home(){
        global $display;
        die("Nothing to do 1212...");
    }
    function get_by_star(){
        global $display;
        $ajax_response = new AjaxRes();
        $star = (int)Url::get('star');
        $limit = 'LIMIT 0,10';
        $condition = '(status = 1) AND (price_min > 0)';
        if($star > 0)
            $condition.= ' AND star='.$star;
        $city = (int)Url::get('city');
        if($city > 0)
            $condition.= ' AND city_id='.$city;
        Db_Location::get_cities();
        $total 	= Db_Hotel::total($condition);
        $hotels = Db_Hotel::getHotelsForHome($limit,$condition);
        $city = array();
        $imgsDefault = array();
        $hotelShow = array();
        foreach($hotels as $h){
            $imgsDefault[] =$h['image_default'];
            $h['feel'] = Db_HotelReview::getFeelByScore($h['rating_0_0_average']);
                $hotelShow[] = $h;
                $city[]=Db_Location::getCityNotDistrict($h['city_id']);
        }     
        $imgsDefault = join(',',$imgsDefault);
        if($imgsDefault){
            $imgs =Db_Image::selectImages('id, img_name, img_url, img_server', '', "id IN ($imgsDefault)");
            $display->add('imgs',$imgs);
        }
        if(sizeof($hotelShow) > 0 ){
            $display->add('hotels', $hotelShow);
            $display->add('total', $total);
            $display->add('city',$city);
            $display->add("WEB_ROOT",WEB_ROOT);
            $display->add("WEB_DIR",WEB_DIR);
            $result = $display->output('slideHotel', true, 'Home');
            $ajax_response->type = AjaxRes::SUCCESS;
                $ajax_response->item = $result;
            
            }else{
                $ajax_response->type = AjaxRes::SUCCESS;
                $ajax_response->item = '<br /><strong style="display: block;text-align: center;">Không có khách sạn nào</strong><br />';
            }
            echo $ajax_response->toString();
    }
    
    
    function get_compare_next(){
        $idcurrent = Url::get('idhotel');
        $id0 = Url::get('id0');
        $id1 = Url::get('id1');
        $index = Url::get('index');
        $limit = 'LIMIT 0,1';
        $id = $idcurrent;
        $a = explode("|",$_SESSION['recent_hotel']);
                $a = array_unique($a);
                $a = array_filter($a);
                $c = array($id0,$id1);
                $a = array_diff($a,$c);
                $check = array();
                $tmp = Db_Hotel::getHotelsForHome('','status != 1');
                foreach ($tmp as $t){
                    $check [] = $t['id'];
                }
                $a = array_diff($a,$check);
        $con = implode($a,',');
        $b = explode(',', $con);//rebuild index to loop

        $i=0;
        while($i<sizeof($b)){
                if($id == $b[$i]){
                    if($i == (sizeof($b)-1))
                        $id = $b[0];
                    else
                        $id = $b[$i+1];
                    break;
                }
                $i++;
        }
        $i=0;
        //$condition = '(status = 1) AND id='.$id;
        Db_Location::get_cities();
        $h = Db_Hotel::getHotelById($id);
        $h['bkname'] = CGlobal::$all_cities[$h['city_id']]['bkname'];
            $h['room1']=FALSE;
            $h['room2']=FALSE;
            $h['room3']=FALSE;
            $h['roomgroup']=FALSE;
            $h['room1price']=1234567890;
            $h['room2price']=1234567890;
            $h['room3price']=1234567890;
            $h['roomgroupprice']=1234567890;
            $rooms = Db_Room::getRoomsByHotelId($h['id']);
            $check = 0;
            foreach ($rooms as $r){
                $check = $r['max_adult'];
                if($check == 1){
                     $h['room1'] = TRUE;
                     if($r['price'] < $h['room1price'])
                         $h['room1price'] = $r['price'];
                }
                if($check == 2){
                     $h['room2'] = TRUE;
                     if($r['price'] < $h['room2price'])
                         $h['room2price'] = $r['price'];
                }
                if($check == 3){
                     $h['room3'] = TRUE;
                     if($r['price'] < $h['room3price'])
                         $h['room3price'] = $r['price'];
                }
                if($check > 3){
                     $h['roomgroup'] = TRUE;
                     if($r['price'] < $h['roomgroupprice'])
                         $h['roomgroupprice'] = $r['price'];
                }
            }
            //tiện nghi
            if($h['filter_ids'] != '')
                $h['filter'] = DB::select_all('hotel_filter','id IN ('.$h['filter_ids'].')');
            else {
                $h['filter'] = '';
            }
            $h['feel'] = Db_HotelReview::getFeelByScore($h['rating_0_0_average']);
            $temp = Db_Image::selectImages('img_url','', "id=".$h['image_default']);
            $h['url_image'] = '';
            foreach ($temp as $tmp){
                $h['url_image'] = $tmp['img_url'];
                break;
            }
                $star = '';
        for($i = 1; $i < $h['star']; $i++)
            $star.='<i class="icon iStar"></i>';
        $td0 = '';
        $td1 = '';
        $td2 = '';
        $td3 = '';
        $td4 = '';
        $td5 = '';
        $td6 = '';
        $td7 = '';
        $td0 .= '<input type="hidden" name="tdhidden'.$index.'-0" id="tdhidden0-'.$index.'" value="'.$h['id'].'">
                            <div class="thumb-hotel">
                                <p class="grp-btn-action">
                                    <a>
                                        <i class="iconCompare iPrevPick" id="pre'.$index.'" title="Trước"></i>
                                    </a>
                                    <a>
                                        <i class="iconCompare iXPick" id="tdclose'.$index.'" title="Xóa"></i>
                                    </a>
                                    <a>
                                        <i class="iconCompare iNextPick" id="next'.$index.'" title="Sau"></i>
                                    </a>
                                </p>
                                <a class="thumb" href="'.$h['bkname'].'/'.$h['slug'].'/'.$h['id'].'">
                                    <img src="'.IMAGE_PATH.'/'.$h['url_image'].'" width="100%" height="130" alt="$HOTEL_NAME">
                                </a>
                                <h4 class="hotel-name">
                                    <a href="'.$h['bkname'].'/'.$h['slug'].'/'.$h['id'].'">'.$h['name'].'</a>
                                </h4>
                                <p class="rating-star">
                                    '.$star.'
                                    <i class="icon iStar"></i>
                                </p>
                                <p class="address">'.$h['address'].'</p>
                            </div>';
        if($h['room1'])
            $td1 .= 'Từ
                            <span class="fs18 fc-red">'.number_format($h['room1price'], 0, '.', '.').'đ</span>';
        else {
            $td1 .= '<i class="iconCompare iX"></i>';
        }
        if($h['room2'])
            $td2 .= 'Từ
                            <span class="fs18 fc-red">'.number_format($h['room2price'], 0, '.', '.').'đ</span>';
        else {
            $td2 .= '<i class="iconCompare iX"></i>';
        }
        if($h['room3'])
            $td3 .= 'Từ
                            <span class="fs18 fc-red">'.number_format($h['room3price'], 0, '.', '.').'đ</span>';
        else {
            $td3 .= '<i class="iconCompare iX"></i>';
        }
        if($h['roomgroup'])
            $td4 .= 'Từ
                            <span class="fs18 fc-red">'.number_format($h['roomgroupprice'], 0, '.', '.').'đ</span>';
        else {
            $td4 .= '<i class="iconCompare iX"></i>';
        }
        
        if($h['rating_0_0_count'] < 5){
            $td5 .= '<i class="iconCompare iMoi"></i>
                            <span class="fs13">Không có đủ đánh giá</span>';
        }
        else{
            $td5 .= '<span class="fw-b fs18">'.$h['rating_0_0_average'].'</span> <span class="fs13">- '.$h['feel'].'</span>';
        }
        
        if($h['rating_0_0_count'] < 5){
            $td6 .= '<span class="fs13"> Ít hơn <span class="fs18 fw-b">5</span> đánh giá</span>';
        }
        else{
            $td6 .= '<span class="fs13"> <span class="fs18 fw-b">'.$h['rating_0_0_count'].'</span> đánh giá</span>';
        }
        if(is_array($h['filter']) && $h['filter'] != ''){
            foreach ($h['filter'] as $f){
                $td7 .= '<p class="item_compare item_'.$f['id'].'" data-key="'.$f['id'].'">'.$f['name'].'</p>';
            }
        }  else {
            $td7 .= '<p>Chưa có thông tin</p>';
        }
        $td7 .= '<script type="text/javascript">
                    $(document).ready(function(){
                        console.log("ok");
                        $(".item_compare").mouseover(function(){
                            var key = $(this).attr("data-key");
                            console.log(key);
                            $(".item_"+key).css("border", "1px #D0D0D0 solid").css("padding", "5px 3px").css("background", "#E7E7E7").css("border-radius", "0.3em");
                        }).mouseout(function(){
                            var key = $(this).attr("data-key");
                            console.log(key);
                            $(".item_"+key).css("border", "").css("padding", "6px 4px").css("background", "").css("border-radius", "");
                        });
                    });
                </script>';
        $result = array(
            'td0' => $td0,  
            'td1' => $td1,
            'td2' => $td2,
            'td3' => $td3,
            'td4' => $td4,
            'td5' => $td5,
            'td6' => $td6,
            'td7' => $td7,
            'id' => $id
        );
        echo json_encode($result);
    }
    
    function get_compare_back(){
        $idcurrent = Url::get('idhotel');
        $id0 = Url::get('id0');
        $id1 = Url::get('id1');
        $index = Url::get('index');
        $limit = 'LIMIT 0,1';
        $id = $idcurrent;
        $a = explode("|",$_SESSION['recent_hotel']);
                $a = array_unique($a);
                $a = array_filter($a);
                $c = array($id0,$id1);
                $a = array_diff($a,$c);
                $check = array();
                $tmp = Db_Hotel::getHotelsForHome('','status != 1');
                foreach ($tmp as $t){
                    $check [] = $t['id'];
                }
                $a = array_diff($a,$check);
        $con = implode($a,',');
        $b = explode(',', $con);//rebuild index to loop
        $i=0;
        while($i<sizeof($b)){
                if($id == $b[$i]){
                    if($i == 0)
                        $id = $b[sizeof($b)-1];
                    else
                        $id = $b[$i-1];
                    break;
                }
                $i++;
        }
        $i=0;
        //$condition = '(status = 1) AND id='.$id;
        Db_Location::get_cities();
        $h = Db_Hotel::getHotelById($id);
        $h['bkname'] = CGlobal::$all_cities[$h['city_id']]['bkname'];
            $h['room1']=FALSE;
            $h['room2']=FALSE;
            $h['room3']=FALSE;
            $h['roomgroup']=FALSE;
            $h['room1price']=1234567890;
            $h['room2price']=1234567890;
            $h['room3price']=1234567890;
            $h['roomgroupprice']=1234567890;
            $rooms = Db_Room::getRoomsByHotelId($h['id']);
            $check = 0;
            foreach ($rooms as $r){
                $check = $r['max_adult'];
                if($check == 1){
                     $h['room1'] = TRUE;
                     if($r['price'] < $h['room1price'])
                         $h['room1price'] = $r['price'];
                }
                if($check == 2){
                     $h['room2'] = TRUE;
                     if($r['price'] < $h['room2price'])
                         $h['room2price'] = $r['price'];
                }
                if($check == 3){
                     $h['room3'] = TRUE;
                     if($r['price'] < $h['room3price'])
                         $h['room3price'] = $r['price'];
                }
                if($check > 3){
                     $h['roomgroup'] = TRUE;
                     if($r['price'] < $h['roomgroupprice'])
                         $h['roomgroupprice'] = $r['price'];
                }
            }
            //tiện nghi
            if($h['filter_ids'] != '')
                $h['filter'] = DB::select_all('hotel_filter','id IN ('.$h['filter_ids'].')');
            else {
                $h['filter'] = '';
            }
            $h['feel'] = Db_HotelReview::getFeelByScore($h['rating_0_0_average']);
            $temp = Db_Image::selectImages('img_url','', "id=".$h['image_default']);
            $h['url_image'] = '';
            foreach ($temp as $tmp){
                $h['url_image'] = $tmp['img_url'];
                break;
            }
                $star = '';
        for($i = 1; $i < $h['star']; $i++)
            $star.='<i class="icon iStar"></i>';
        $td0 = '';
        $td1 = '';
        $td2 = '';
        $td3 = '';
        $td4 = '';
        $td5 = '';
        $td6 = '';
        $td7 = '';
        $td0 .= '<input type="hidden" name="tdhidden'.$index.'-0" id="tdhidden0-'.$index.'" value="'.$h['id'].'">
                            <div class="thumb-hotel">
                                <p class="grp-btn-action">
                                    <a>
                                        <i class="iconCompare iPrevPick" id="pre'.$index.'" title="Trước"></i>
                                    </a>
                                    <a>
                                        <i class="iconCompare iXPick" id="tdclose'.$index.'" title="Xóa"></i>
                                    </a>
                                    <a>
                                        <i class="iconCompare iNextPick" id="next'.$index.'" title="Sau"></i>
                                    </a>
                                </p>
                                <a class="thumb" href="'.$h['bkname'].'/'.$h['slug'].'/'.$h['id'].'">
                                    <img src="'.IMAGE_PATH.'/'.$h['url_image'].'" width="100%" height="130" alt="$HOTEL_NAME">
                                </a>
                                <h4 class="hotel-name">
                                    <a href="'.$h['bkname'].'/'.$h['slug'].'/'.$h['id'].'">'.$h['name'].'</a>
                                </h4>
                                <p class="rating-star">
                                    '.$star.'
                                    <i class="icon iStar"></i>
                                </p>
                                <p class="address">'.$h['address'].'</p>
                            </div>';
        if($h['room1'])
            $td1 .= 'Từ
                            <span class="fs18 fc-red">'.$h['room1price'].'đ</span>';
        else {
            $td1 .= '<i class="iconCompare iX"></i>';
        }
        if($h['room2'])
            $td2 .= 'Từ
                            <span class="fs18 fc-red">'.$h['room2price'].'đ</span>';
        else {
            $td2 .= '<i class="iconCompare iX"></i>';
        }
        if($h['room3'])
            $td3 .= 'Từ
                            <span class="fs18 fc-red">'.$h['room3price'].'đ</span>';
        else {
            $td3 .= '<i class="iconCompare iX"></i>';
        }
        if($h['roomgroup'])
            $td4 .= 'Từ
                            <span class="fs18 fc-red">'.$h['roomgroupprice'].'đ</span>';
        else {
            $td4 .= '<i class="iconCompare iX"></i>';
        }
        
        if($h['rating_0_0_count'] < 5){
            $td5 .= '<i class="iconCompare iMoi"></i>
                            <span class="fs13">Không có đủ đánh giá</span>';
        }
        else{
            $td5 .= '<span class="fw-b fs18">'.$h['rating_0_0_average'].'</span> <span class="fs13">- '.$h['feel'].'</span>';
        }
        
        if($h['rating_0_0_count'] < 5){
            $td6 .= '<span class="fs13"> Ít hơn <span class="fs18 fw-b">5</span> đánh giá</span>';
        }
        else{
            $td6 .= '<span class="fs13"> <span class="fs18 fw-b">'.$h['rating_0_0_count'].'</span> đánh giá</span>';
        }
        if(is_array($h['filter']) && $h['filter'] != ''){
            foreach ($h['filter'] as $f){
                $td7 .= '<p class="item_compare item_'.$f['id'].'" data-key="'.$f['id'].'">'.$f['name'].'</p>';
            }
        }  else {
            $td7 .= '<p>Chưa có thông tin</p>';
        }
        $td7 .= '<script type="text/javascript">
                    $(document).ready(function(){
                        console.log("ok");
                        $(".item_compare").mouseover(function(){
                            var key = $(this).attr("data-key");
                            console.log(key);
                            $(".item_"+key).css("border", "1px #D0D0D0 solid").css("padding", "5px 3px").css("background", "#E7E7E7").css("border-radius", "0.3em");
                        }).mouseout(function(){
                            var key = $(this).attr("data-key");
                            console.log(key);
                            $(".item_"+key).css("border", "").css("padding", "6px 4px").css("background", "").css("border-radius", "");
                        });
                    });
                </script>';
        $result = array(
            'td0' => $td0,  
            'td1' => $td1,
            'td2' => $td2,
            'td3' => $td3,
            'td4' => $td4,
            'td5' => $td5,
            'td6' => $td6,
            'td7' => $td7,
            'id' => $id
        );
        echo json_encode($result);
    }
    function remove_id_recent(){
        $id = Url::get('idh','');
        if($id != ''){
            $tmp = explode("|",$_SESSION['recent_hotel']);
            $temp = array_diff($tmp, array($id));
            if(sizeof($temp) >= 3){
                $_SESSION['recent_hotel']= implode($temp,'|');// set new value for session
                echo 'done '.$_SESSION['recent_hotel'];
                exit;
            }
        }else {
            echo 'none';
            exit;
        }
    }
}
?>